package com.jx.flowershopmanager.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jx.flowershopmanager.R;
import com.jx.flowershopmanager.entity.FlowersEntity;
import com.jx.flowershopmanager.entity.ProductEntity;
import com.jx.flowershopmanager.home.activity.ProductActivity;
import com.jx.flowershopmanager.model.Flowers;

import java.util.List;

public class MyGdAdapter extends BaseAdapter {
    private List<Flowers.Data> flowersList;
    private Context context;

    public MyGdAdapter(List<Flowers.Data> flowersList, Context context) {
        this.flowersList = flowersList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return flowersList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if(convertView==null){
            holder= new ViewHolder();
            convertView=View.inflate(context, R.layout.grid_view_item,null);
            holder.textName =(TextView) convertView.findViewById(R.id.grid_txt);
            holder.rootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            holder.textDescribe =(TextView) convertView.findViewById(R.id.describe);
            holder.textStore =(TextView) convertView.findViewById(R.id.store);
            holder.textSold =(TextView) convertView.findViewById(R.id.sold);
            holder.imageView=(ImageView) convertView.findViewById(R.id.photo_view);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder) convertView.getTag();
        }
        holder.textName.setText(flowersList.get(i).getfName());
        holder.textDescribe.setText(flowersList.get(i).getDescribe());
        holder.textStore.setText(String.valueOf(flowersList.get(i).getPrice()));
        holder.textSold.setText(String.valueOf(flowersList.get(i).getOrigin()));
        Glide.with(context)
                .load(flowersList.get(i).getUrl())
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .into(holder.imageView);
        holder.rootLayout.setOnClickListener(v -> {
            Intent intent = new Intent(context, ProductActivity.class);
            intent.putExtra("id",flowersList.get(i).getfId());
            context.startActivity(intent);
        });
        return convertView;
    }

    static class ViewHolder{
        LinearLayout rootLayout;
        ImageView imageView;
        TextView textName;
        TextView textDescribe;
        TextView textStore;
        TextView textSold;
    }

}